package com.common.frame.utils;

import java.util.concurrent.Executors;

import io.reactivex.ObservableTransformer;
import io.reactivex.Scheduler;
import io.reactivex.SingleTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class TransformUtils {

    public static <T> ObservableTransformer<T, T> defaultObservableSchedulers() {
        return tObservable -> tObservable
                .unsubscribeOn(getScheduler())
                .subscribeOn(getScheduler())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public static <T> ObservableTransformer<T, T> workObservableSchedulers() {
        return tObservable -> tObservable
                .unsubscribeOn(getScheduler())
                .subscribeOn(getScheduler())
                .observeOn(getScheduler());
    }

    public static <T> ObservableTransformer<T, T> mainObservableSchedulers() {
        return tObservable -> tObservable
                .unsubscribeOn(AndroidSchedulers.mainThread())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public static <T> SingleTransformer<T, T> defaultSingleSchedulers() {
        return tObservable -> tObservable
                .unsubscribeOn(getScheduler())
                .subscribeOn(getScheduler())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public static <T> SingleTransformer<T, T> mainSingleSchedulers() {
        return tObservable -> tObservable
                .unsubscribeOn(AndroidSchedulers.mainThread())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread());
    }

    private static Scheduler mScheduler;
    private static Scheduler getScheduler() {
        if (mScheduler == null) {
            synchronized (TransformUtils.class) {
                if (mScheduler == null) {
                    mScheduler = Schedulers.from(Executors.newFixedThreadPool(15));
                }
            }
        }
        return mScheduler;
    }



}
